/*
 * Copyright (C) 2010 Grummfy
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package org.android.bbox.data;

import org.android.bbox.AbstractActivity;
import org.android.bbox.utils.CursorUtils;

import android.app.ListActivity;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class Profil
{
	private String ip;
	private String name;
	private int id;
	private String login;
	private String password;
	private String password_user;
	private boolean save_password;
	private String image;
	private String color;

	private ContentValues update;

	private ListActivity a;

	/**
	 * Create a new Profil entry
	 * @param ip
	 * @param name
	 * @param login
	 * @param password
	 * @param passwordUser
	 * @param savePassword
	 * @param image
	 * @param color
	 * @return new profil id 
	 */
	public static int create(ContentResolver cr, String ip, String name, String login, String password, String passwordUser, boolean savePassword, String image, String color)
	{
		ContentValues values = new ContentValues();
		values.put("ip", ip);
		values.put("name", name);
		values.put("login", login);
		values.put("password", password);
		values.put("password_user", passwordUser);
		values.put("save_password", savePassword);
		values.put("image", image);
		values.put("color", color);
		Uri uri = cr.insert(ProfilProvider.CONTENT_URI, values);

		//XXX debug
		System.out.println();
		System.out.println("-----------------------");
		System.out.println("New content created!");
		System.out.println(uri);
		System.out.println("-----------------------");
		System.out.println();

		//TODO return id or ....
		return 0;
	}

	public Profil(ListActivity a, int profileId) throws Exception
	{
		Uri pId = ProfilProvider.CONTENT_URI;

		this.a = a;
		
		String[] selectionArgs = {""+profileId};
		Cursor c = this.a.managedQuery(pId, null, "id = ?", selectionArgs, null);

		if (!c.moveToFirst() || c.getCount() <= 0)
		{
			throw new Exception("No data");
		}

		this.ip = c.getString(c.getColumnIndex("ip"));
		this.name = c.getString(c.getColumnIndex("name"));
		this.login = c.getString(c.getColumnIndex("login"));
		this.password = c.getString(c.getColumnIndex("password"));
		this.password_user = c.getString(c.getColumnIndex("password_user"));
		this.image = c.getString(c.getColumnIndex("image"));
		this.color = c.getString(c.getColumnIndex("color"));
		this.save_password = (c.getInt(c.getColumnIndex("save_password")) > 0);
		this.id = c.getInt(c.getColumnIndex("id"));
	}

	public void update()
	{
		this.a.getContentResolver().update(Uri.parse(ProfilProvider.CONTENT_URI.toString() + "/" + this.id), this.update, null, null);
	}

	public void delete()
	{
		this.a.getContentResolver().delete(Uri.parse(ProfilProvider.CONTENT_URI.toString() + "/" + this.id), null, null);
	}

	//
	// Getters
	//
	public int getId()
	{
		return this.id;
	}

	public String getImage()
	{
		//setImageResource
		return this.image;
	}

	public String getColor()
	{
		return this.color;
	}

	public String getIp()
	{
		return this.ip;
	}

	public String getName()
	{
		return this.name;
	}

	public String getLogin()
	{
		return this.login;
	}

	public String getPassword()
	{
		return this.password;
	}

	public String getPassword_user()
	{
		return this.password_user;
	}

	public boolean isSave_password()
	{
		return this.save_password;
	}

	//
	// Setters
	//
	public void setIp(String ip)
	{
		this.setStringKey("ip", ip);
	}

	public void setName(String name)
	{
		this.setStringKey("name", name);
	}

	public void setLogin(String login)
	{
		this.setStringKey("login", login);
	}

	public void setPassword(String password)
	{
		this.setStringKey("password", password);
	}

	public void setPassword_user(String passwordUser)
	{
		this.setStringKey("password_user", passwordUser);
	}

	public void setImage(String image)
	{
		this.setStringKey("image", image);
	}

	public void setColor(String color)
	{
		this.setStringKey("color", color);
	}

	public void setSave_password(boolean savePassword)
	{
		this.getContentValues().put("save_password", savePassword);
	}

	public String toString()
	{
		return "Profile : " + this.getName() + "(" + this.getId() + ") to access " + this.getIp();
	}

	/**
	 * Get the ContentValues for update
	 */
	private ContentValues getContentValues()
	{
		if (this.update == null)
		{
			this.update = new ContentValues();
		}
		return this.update;
	}

	private void setStringKey(String key, String value)
	{
		this.getContentValues().put(key, value);
	}
}
